import * as types from './mutaions-type';
import Vue from 'vue';
import userdata from '@/constdata/router/userdata';
import routerdata from '@/constdata/router/routerdata';
import session from '@/toolset/session-storage/session';
import { login } from '@/api';
import { Toast } from 'vue-ydui/dist/lib.rem/dialog';

export default {
  state: {
    username: session.getItem('USERNAME'),
    role: session.getItem('ROLE'),
    routers: session.getItem('ROUTERS')
  },
  getters: {
    role: state => state.role,
    routers: state => state.routers
  },
  mutations: {
    /*
     * 设置用户拥有的路由
     * 作者：gzt
     */

    [types.SET_ROUTER]: (state, routers) => {
      state.routers = routers;
      session.setItem('ROUTERS', routers);
    },

    /*
     * 设置角色
     * 作者：gzt
     */

    [types.SET_ROLE]: (state, role) => {
      state.role = role;
      session.setItem('ROLE', role);
    },

    /*
     * 设置用户名
     * 作者：gzt
     */

    [types.SET_USERNAME]: (state, user_name) => {
      state.username = user_name;
      session.setItem('USERNAME', user_name);
    }
  },
  actions: {
    /*
     * 登录的操作
     * 2019年8月31日
     * 作者：gzt
     */
    Logins({ state, commit, rootState }, info) {
      return new Promise((resolve, reject) => {
        let data = {};
        // 发送后台请求，获取路由数据
        if (rootState.runtime !== 'debuger') {
          let user = userdata.users.find(
            ent => ent.login_name == info.username
          );
          if (user) {
            if (user.password === info.pwd) {
              //查找当前用户的所有权限
              commit('SET_ROUTER', routerdata.routers);
              commit('SET_ROLE', user.role);
              commit('SET_USERNAME', user.login_name);
              localStorage.setItem('userData', JSON.stringify(user));
              data = {
                user_name: user.login_name,
                role: user.role
              };
              resolve(data);
            } else {
              reject('密码错误');
            }
          } else {
            reject('用户名不存在');
          }
        } else {
          //发送真实的网络请求
          const { username: account, pwd: password } = info;
          login({ account, password })
            .then(res => {
              if (res && res.success) {
                commit('SET_ROUTER', routerdata.routers);
                commit('SET_ROLE', []);
                commit('SET_USERNAME', res.data.account);
                localStorage.setItem('userData', JSON.stringify(res.data));
                localStorage.setItem('token', res.data);
                resolve(res);
              } else
                Toast({
                  mes: res.msg,
                  timeout: 1500,
                  icon: 'error'
                });
            })
            .catch(err => reject(err));
        }
      });
    },

    /*
     * 储存拥有的权限路由
     * 2019年8月31日
     * 作者：gzt
     * */

    AddRoute({ commit }, route) {
      return new Promise((resolve, reject) => {
        commit('SET_ROUTER', route);
        resolve(route);
      }).catch(error => {
        reject(error);
      });
    },

    /*
     * 退出登录
     * 2019年8月31日
     * 作者：gzt
     * */
    Logout({ commit, state }) {
      return new Promise((resolve, reject) => {
        commit('SET_USERNAME', '');
        commit('SET_ROLE', '');
        commit('SET_ROUTER', []);
        localStorage.removeItem('userData');
        localStorage.removeItem('token');
        location.reload();
        resolve();
      }).catch(error => {
        reject(error);
      });
    }
  }
};
